package com.example.d_app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText edit_username;
    private EditText edit_password;
    private CheckBox checkBox2;
    private Button button_regist;
    private Button button_login;
    private Button button_cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        edit_username = (EditText) findViewById(R.id.edit_username);
        edit_password = (EditText) findViewById(R.id.edit_password);
        checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
        button_regist = (Button) findViewById(R.id.button_regist);
        button_login = (Button) findViewById(R.id.button_login);
        button_cancel = (Button) findViewById(R.id.button_cancel);

        button_regist.setOnClickListener(this);
        button_login.setOnClickListener(this);
        button_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {


        Intent intent;
        switch (v.getId()) {
            case R.id.button_regist:
                intent=new Intent(this,Regist.class);
                startActivity(intent);
                break;
            case R.id.button_login:
                // validate
                String username = edit_username.getText().toString().trim();
                if (TextUtils.isEmpty(username)) {
                    Toast.makeText(this, "username不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                String password = edit_password.getText().toString().trim();
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(this, "password不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                User user=new User();
                user.getUserBySharedPreferences(this);
                if(username.equals(user.getName())&&password.equals(user.getPwd())){
                    Boolean flag=checkBox2.isChecked();
                    User user2;
                    if(flag){
                        user=new User(username,password,user.getPhone(),user.getAdd(),flag);
                    }else {
                        user=new User();
                    }
                    user.saveUserBySharedPreferences(this);
                     intent=new Intent(MainActivity.this,Welcome.class);
                    Bundle bundle=new Bundle();
                    intent.putExtra("username",username);
                    startActivity(intent);
                }else {
                    Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                    return;
                }
                break;
            case R.id.button_cancel:
                edit_username.setText("");
                edit_password.setText("");
                break;
        }
    }



    @Override
    protected void onResume() {
        Log.i("tag","onResume");
        User user=new User();
        //SharedPreferences方式自动填写
        user.getUserBySharedPreferences(this);
        edit_username.setText( user.getName());
        edit_password.setText( user.getPwd());
        checkBox2.setChecked(user.getFlag());
        super.onResume();
    }
}
